package com.heima.behavior.controller;

import com.heima.behavior.service.AdBehaviorService;
import com.heima.client.article.IArticleClient;
import com.heima.model.common.dtos.*;
import com.heima.utils.thread.ApThreadLocalUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
@RequestMapping("/api/v1")
@Api("用户行为")
public class AdBehaviorController {
    @Autowired
    private AdBehaviorService adBehaviorService;
    @Autowired
    private IArticleClient articleClient;

    /**
     * 点赞/取消点赞行为
     * @param dto
     * @return
     */
    @PostMapping("/likes_behavior")
    public ResponseResult like(@RequestBody LikesBehaviorDto dto) {
        log.info("收到点赞请求:{}", dto);
        return adBehaviorService.like(dto);
    }

    /**
     * 不喜欢添加 行为
     * @param
     *
     * @param
     * @return
     */
    @PostMapping("/un_likes_behavior")
    public ResponseResult dislike(@RequestBody UnLikesBehaviorDto unLikeDto) {
        log.info("收到不喜欢请求:{}", unLikeDto);
        return adBehaviorService.unLike(unLikeDto);
//        return null;
    }

    /**
     * 用户阅读行为
     *
     * @param behaviorDto
     * @return
     */
    @PostMapping("/read_behavior")
    @ApiOperation("用户的阅读行为")
    public ResponseResult readBehavior(@RequestBody ReadBehaviorDto behaviorDto) {

        Integer userId = ApThreadLocalUtil.get();
        behaviorDto.setUserId(String.valueOf(userId));
        log.info("接收到阅读行为：{}", behaviorDto);
        return articleClient.updateArticle(behaviorDto);
    }
    /**
     * 用户收藏行为
     */
    @PostMapping("/collection_behavior")
    @ApiOperation("用户的收藏行为")
    public ResponseResult collectionBehavior(@RequestBody CollectionBehaviorDto behaviorDto) {
        log.info("接收到收藏行为：{}", behaviorDto);
        return adBehaviorService.updateCollection(behaviorDto);
    }
}
